home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / basic / bitstuf.com / BITSTUF2.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-08-12  |  3.4 KB  |  122 lines

  1.     page 66, 132
  2.     title Binary (bit) oriented STR and VAL for BASIC
  3.     subttl By Jim Mack, Editing Services Co.
  4.  
  5. odg    equ    <offset dgroup>
  6.  
  7. comment |
  8.  
  9.     Implements routines to generate and interpret binary strings.
  10.  
  11.     Two procedures are declared in BASIC:
  12.  
  13.     DECLARE FUNCTION BitStr$ (word%)
  14.     DECLARE FUNCTION BitVal& (ones$)  'or BitVal% (ones$)
  15.  
  16.     BitStr returns a 16-byte string in the form "0010010000111001"
  17.      with 1's corresponding to set bits in WORD%
  18.  
  19.     BitVal returns the value of a string such as the one above.  Only the
  20.      first 16 places in the string are evaluated.  Fewer may be used.
  21.      A null string returns a zero result.
  22.  
  23.     NOTE: BitVal can be declared as an integer function instead, since
  24.     the entire value is returned in the lower 16 bits.  If you use it
  25.     that way, returned vales are signed integers: a string whose highest
  26.     bit is set will return a negative value.  As a long integer function,
  27.     values are always positive.
  28.  
  29.     | comment ends
  30.  
  31.     page+
  32.     .model medium, basic
  33.                                     page+
  34.     .data
  35.  
  36. ; This is a string descriptor and associated string body for our use
  37.  
  38. gstr    dw    16        ;this is a valid QB string descriptor...
  39.     dw    xstr
  40.  
  41. xstr    db    16 dup (0)    ;...which refers to this string
  42.  
  43.     .code
  44.  
  45. BitStr        PROC uses es di, vlu
  46.  
  47.     cld
  48.     mov    ax, ds
  49.     mov    es, ax            ; this so "stosb" will work for sure
  50.     assume    es:@data
  51.     mov    bx, vlu
  52.     mov    dx, [bx]        ; dx = word containing bits to test
  53.     mov    cx, gstr        ; number of bit positions
  54.     mov    di, odg:xstr        ; point to beginning of our own string
  55.     mov    ah, '0'
  56. @@:    mov    al, ah            ; for each bit, start with "0"
  57.     shl    dx, 1            ; shift a bit into carry
  58.     adc    al, 0            ; makes "0" into "1" if bit is set
  59.     stosb                ; put "0" or "1" into string
  60.     loop    @b            ; until all 16 bits tested
  61.     mov    ax, odg:gstr        ; pointer to our own descriptor
  62.     ret
  63.  
  64. BitStr        ENDP
  65.                                     page+
  66. BitVal        PROC uses es di si, str
  67.  
  68. ;  The value is taken in two steps: first, the passed string is right-
  69. ;  justified (RSET) into a local string accumulator of exactly 16 bytes,
  70. ;  with ASCII bias removed.  Then the local string is evaluated to a
  71. ;  numeric result.  This allows strings of fewer than 16 bytes to be
  72. ;  aligned correctly, and illegal values to be detected easily.
  73.  
  74.     cld
  75.     mov    ax, ds
  76.     mov    es, ax
  77.     assume    es:@data
  78.     xor    ax, ax            ; clear result in case no string
  79.     mov    bx, str
  80.     mov    cx, [bx]        ; length of passed string
  81.     or    cx, cx
  82.     jz    bv99            ; null string passed
  83.     mov    di, odg:xstr        ; will end up pointing off the end
  84.     mov    cx, 8
  85.     rep    stosw            ; zero our string accumulator
  86.     mov    cx, [bx]
  87.     cmp    cx, 16            ; if < 16, use passed length
  88.     jna    @f
  89.     mov    cx, 16            ; maximum string is 16 characters
  90. @@:    mov    si, 2[bx]        ; address of string data
  91.     add    si, cx
  92.     std                ; work backwards...
  93.     dec    si            ; ...from ends of strings...
  94.     dec    di            ; ...to right-align the data
  95.     xor    bx, bx            ; clear numeric accumulator
  96. @@:    lodsb                ; get string byte
  97.     cmp    al, '1'            ; abort on illegal characters
  98.     ja    bv90
  99.     cmp    al, '0'
  100.     jb    bv90
  101.     and    al, 1            ; remove ASCII bias
  102.     stosb                ; put into string accumulator
  103.     loop    @b
  104.     cld                ; string accumulator now full
  105.     mov    cx, 16
  106.     mov    si, odg:xstr        ; turn it into numeric equivalent
  107. @@:    lodsb
  108.     xor    ah, ah
  109.     sub    ah, al            ; set carry for rotate if '1'
  110.     rcl    bx, 1            ; set bit in result
  111.     loop    @b            ; repeat until string empty
  112. bv90:
  113.     mov    ax, bx            ; accumulator to result
  114. bv99:
  115.     xor    dx, dx            ; make valid for INT or LONG
  116.     cld                ; clean up flags or die later
  117.     ret
  118.  
  119. BitVal        ENDP
  120.  
  121.     END
  122.